
import os, Image
import Config

class Repository:

	__single = None
	
	def __init__(self):

		if Repository.__single: # singleton
			raise Repository.__single
		Repository.__single = self

		self.path = Config.REPOSITORY
		if not os.path.exists(self.path):
			os.mkdir(self.path)


	def getThumbnail(self, imgPath):

		thumbPath = str(self.path + imgPath)
		thumbDir = os.path.dirname(thumbPath)

		if not os.path.exists(thumbDir):
			os.makedirs(thumbDir)
		if not os.path.exists(thumbPath):
			self.__createThumbnail(imgPath, thumbPath)

		return thumbPath


	def __createThumbnail(self, imgPath, thumbPath):

		fp_im = open(imgPath, 'r')
		fp_thumb = open(thumbPath, 'w')

		im = Image.open(fp_im)
		im.thumbnail((Config.THUMBWIDTH, Config.THUMBWIDTH), Image.ANTIALIAS)
		im.save(fp_thumb, "JPEG")

		fp_im.close()
		fp_thumb.close()



if __name__ == "__main__":
	print "Testing Repository-class"
	imPath = '/home/mads/PythonQt/pictures/playa-del-carmen-2-300x225.jpg'
	repo = Repository()
	repo.getThumbnail(imPath)



